Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

290
Vistas
Need Help properly assigning a Date to a Date[] without modifying existing elements in Typescript Array

I have the following loop that checks if the date is holiday and thereon, fills up the Date[].

if (
          this._dateHelperService.isBusinessDay(temporalDate) &&
          !this._dateHelperService.isHoliday(temporalDate)
        ) {
          mNumberOfDays += 1;
          console.log(temporalDate.toDateString());
          console.log('entre')
          let tmpDate: Date[] = [...result.datesToPaint, temporalDate];
          result.datesToPaint = tmpDate;
          console.table(result.datesToPaint)
          // console.log(mNumberOfDays);
        }
        // re creating the date using a helper service
        if (mNumberOfDays !== numberOfDays) {
           temporalDate = 
           this._dateHelperService.addDays(temporalDate,1);
        }

        // helper service:
        addDays(date: Date, days: number): Date {
            date.setDate(date.getDate() + days);
            return date;
        }

The issue is that every time I assign a new element all the list data gets modified to the latest element. Here is a snapshot of the issue:

Sample output

I'm aware this may be related to the pointer I have on the global list I created at the start of the method for result.datesToPaint. I tried using the spread operator and assigning the date to another date temporally. Still not working. Thanks for the help!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Thanks to @Tobias S. The solution was to adjust my addDays method to the following:

addDays(date: Date, days: number): Date {
    return new Date(date.setDate(date.getDate() + days));
  }

:) Be careful when populating date arrays!

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda